package com.clg.common.config;

/**
 * Created by usercai on 2016/9/25.
 */
import com.clg.common.interceptor.LoginInterceptor;
import com.clg.common.model._MappingKit;
import com.clg.common.velocity.VelocityLayoutRender;
import com.jfinal.config.*;
import com.jfinal.i18n.I18nInterceptor;
import com.jfinal.kit.PropKit;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.activerecord.CaseInsensitiveContainerFactory;
import com.jfinal.plugin.activerecord.dialect.MysqlDialect;
import com.jfinal.plugin.activerecord.tx.TxByMethodRegex;
import com.jfinal.plugin.c3p0.C3p0Plugin;
import com.jfinal.render.RenderFactory;
import com.jfinal.render.ViewType;

public class AppConfig extends com.jfinal.config.JFinalConfig{
    public void configConstant(Constants me) {
        me.setDevMode(true);
        me.setBaseViewPath("/WEB-INF/views");
        RenderFactory.me().setMainRenderFactory(new VelocityLayoutRender.VelocityLayoutRenderFactory());//me.setViewType(ViewType.VELOCITY);
        //me.setViewType(ViewType.VELOCITY);
    }
    public void configRoute(Routes me) {
        me.add(new AppRoute());
    }

    public void configPlugin(Plugins me) {
        PropKit.use("app_config.txt");//第一次使用use加载的配置将成为主配置，可以通过PropKit.get(...)直接取值. 后面要用的得需要 PropKit.use
        C3p0Plugin cp = new C3p0Plugin(PropKit.get("jdbcUrl"), PropKit.get("user"), PropKit.get("password"));
        //配置Oracle驱动
        cp. setDriverClass("com.mysql.jdbc.Driver");
        me.add(cp);
        ActiveRecordPlugin arp = new ActiveRecordPlugin(cp);
        me.add(arp);
        // 配置Oracle方言
        arp.setDialect(new MysqlDialect());
        arp.setContainerFactory(new CaseInsensitiveContainerFactory());
        _MappingKit.mapping(arp);
    }
    public void configInterceptor(Interceptors me) {
        //事物拦截
        me.add(new TxByMethodRegex("(.*save.*|.*update.*)"));
        //国际化
        //me.add(new I18nInterceptor());
        //登录拦截
        me.add(new LoginInterceptor());
    }
    public void configHandler(Handlers me) {
        me.add(new AppHandler());
    }
}
